package gameAudio;

import java.io.*;
import javax.sound.midi.*;
import javax.swing.*;

/**
 * @author Logical Solutions
 **/

public class Audio
{
	private int roomValue;
	private Sequence sequence;
	private Sequencer sequencer;
	
	public void stopMusic()
	{
		try
		{
			sequencer.stop();
			sequencer.close();
		}
		catch(NullPointerException e)
		{}
		catch(IllegalStateException e)
		{}	
	}
	
	public void playMusic(final File file, final int rV)
	{
		new Thread(new Runnable()
			{
				public void run()
				{
					SwingUtilities.invokeLater(new Runnable()
					{
						public void run()
						{
							try{
								if(roomValue != rV)
								{
									try
									{
										sequencer.stop();
										sequencer.close();
									}
									catch(NullPointerException e)
									{}
									catch(IllegalStateException e)
									{}
								}
								roomValue = rV;
								
								sequence = MidiSystem.getSequence(file);
								sequencer = MidiSystem.getSequencer();
								
								sequencer.open();								
								sequencer.setSequence(sequence);
								sequencer.start();
								sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
							}
							catch (IOException e) 
							{
						    }
							catch (MidiUnavailableException e) 
							{
						    }
							catch (InvalidMidiDataException e) 
							{
						    }
						}
					});
				}
			}).start();
	}
}

